
//
//  UITableViewCell+Separator.m
//  HHPLiveVideo
//
//  Created by 韩红坡 on 2017/4/19.
//  Copyright © 2017年 韩红坡. All rights reserved.
//

#import "UITableViewCell+Separator.h"

#import "objc/runtime.h"

@interface UITableViewCell ()

@property (nonatomic, strong) UIView *hhp_separator;

@end

@implementation UITableViewCell (Separator)

- (UIView *)hhp_separator
{
    UIView *separator = objc_getAssociatedObject(self, @selector(hhp_separator));
    if (!separator) {
        separator = [UIView new];
        separator.backgroundColor = [UIColor colorWithRGBString:@"#E2E6E9"];
        objc_setAssociatedObject(self, @selector(hhp_separator), separator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return separator;
}

- (void)setHhp_showSeparator:(BOOL)hhp_showSeparator
{
    if (hhp_showSeparator == self.hhp_showSeparator) {
        return;
    }
    
    objc_setAssociatedObject(self, @selector(hhp_showSeparator), @(hhp_showSeparator), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if (!self.hhp_separator.superview) {
        [self.contentView addSubview:self.hhp_separator];
        [self hhp_updateSeparatorConstraints];
    }
    self.hhp_separator.hidden = !hhp_showSeparator;
}

- (BOOL)hhp_showSeparator
{
    return [objc_getAssociatedObject(self, @selector(hhp_showSeparator)) boolValue];
}

- (void)setHhp_separatorleft:(CGFloat)hhp_separatorleft
{
    if (self.hhp_separatorleft == hhp_separatorleft) {
        return;
    }
    
    objc_setAssociatedObject(self, @selector(hhp_separatorleft), @(hhp_separatorleft), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self hhp_updateSeparatorConstraints];
}

- (CGFloat)hhp_separatorleft
{
    return [objc_getAssociatedObject(self, @selector(hhp_separatorleft)) floatValue];
}

- (void)setHhp_separatorRight:(CGFloat)hhp_separatorRight
{
    if (self.hhp_separatorRight == hhp_separatorRight) {
        return;
    }
    
    objc_setAssociatedObject(self, @selector(hhp_separatorRight), @(hhp_separatorRight), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self hhp_updateSeparatorConstraints];
}

- (CGFloat)hhp_separatorRight
{
    return [objc_getAssociatedObject(self, @selector(hhp_separatorRight)) floatValue];
}

- (void)hhp_updateSeparatorConstraints
{
    if (self.hhp_separator.superview) {
        [self.hhp_separator mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.hhp_separatorleft);
            make.right.mas_equalTo(-self.hhp_separatorRight);
            make.bottom.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
        }];
    }
}

@end
